package cn.itcast.service.system.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.system.DeptDao;
import cn.itcast.domain.system.Dept;
import cn.itcast.service.system.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public List<Dept> findAll(String companyId) {
        return deptDao.findAll(companyId);
    }

    @Override
    public Dept findById(String id) {
        return deptDao.findById(id);
    }

    @Override
    public void save(Dept dept) {
        // 设置主键
        dept.setId(IdUtil.simpleUUID());
        deptDao.save(dept);
    }

    @Override
    public void update(Dept dept) {
        deptDao.update(dept);
    }

    @Override
    public void delete(String id) {
        // 当前部门id，根据id查询子部门   select count(*) from pe_dept where parent_id ，结果大于0，就不允许删除
        deptDao.delete(id);
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize,String companyId) {
        // 1.开启分页插件
        PageHelper.startPage(pageNum, pageSize);
        // 2.查询所有
        List<Dept> list = deptDao.findAll(companyId);
        // 3.封装分页对象
        return new PageInfo(list);
    }
}
